//
//  SSContentViewController.m
//  starspaceVR
//
//  Created by 曹端芹 on 17/9/22.
//  Copyright © 2017年 starspace. All rights reserved.
//

#import "SSContentViewController.h"
#import "SSListConentViewController.h"
#define TitleScrollH 44

@interface SSContentViewController () <UIScrollViewDelegate>
@property (nonatomic,strong) NSArray *titleArray;
@property (nonatomic,weak) UIScrollView *titleScrollView;
@property (nonatomic,weak) UIView *indexLine;
@property (nonatomic,weak) UIScrollView *contentScrollView;
@property (nonatomic,strong) NSMutableArray *titleBtnArray;
@property (nonatomic,strong) UIButton *selectBtn;

@end

@implementation SSContentViewController

- (NSArray *)titleArray {
    if (!_titleArray) {
        _titleArray = @[@"游戏",@"旅游",@"校园",@"房产",@"企业"];
    }
    return _titleArray;

}

- (NSMutableArray *)titleBtnArray {
    if (!_titleBtnArray) {
        _titleBtnArray = [NSMutableArray array];
    }
    return _titleBtnArray;
}

- (void) setupTitlesScrollView {
    
    UIScrollView *titleScrollView = [[UIScrollView alloc] init];
    titleScrollView.backgroundColor = [UIColor whiteColor];
    titleScrollView.frame = CGRectMake(0, 0, kScreenWidth, TitleScrollH);
    titleScrollView.showsHorizontalScrollIndicator = NO;
    titleScrollView.scrollEnabled = NO;
    [self.view addSubview:titleScrollView];
    _titleScrollView = titleScrollView;
    
    
}

- (void) setupIndexLine {
    UIView *line = [[UIView alloc] init];
    line.frame = CGRectMake(0, TitleScrollH, 80, 1);
    line.backgroundColor = [UIColor redColor];
    [self.view addSubview:line];
    _indexLine = line;

}

- (void) setupContentScrollView {
    UIScrollView *contentScrollView = [[UIScrollView alloc] init];
    contentScrollView.frame = CGRectMake(0, TitleScrollH + 5, kScreenWidth, kScreenHeight- TitleScrollH-120);
    contentScrollView.pagingEnabled = YES;
    contentScrollView.bounces = NO;
    contentScrollView.delegate = self;
    contentScrollView.showsVerticalScrollIndicator = NO;
    contentScrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:contentScrollView];
    _contentScrollView = contentScrollView;
}

- (void) setupAllChildViewController {

    for (int i = 0; i < self.titleArray.count; i ++) {
        SSListConentViewController *contentVC = [[SSListConentViewController alloc] init];
        [self addChildViewController:contentVC];
        if (i == 0) {
            contentVC.categoryId = 26;
        }else if(i == 1){
            contentVC.categoryId = 27;
        }else if ( i == 2){
            contentVC.categoryId = 28;
        }else if(i == 3){
            contentVC.categoryId = 52;
        }else{
            contentVC.categoryId = 53;
        }
    }

}

- (void) setupAllTitle {
    
    NSInteger count = self.titleArray.count;
    CGFloat btnW = 80;
    CGFloat btnH = self.titleScrollView.frame.size.height;
    CGFloat btnX = 0;
    for (int i = 0; i < count; i ++) {
        UIButton *titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [titleBtn setTitle:self.titleArray[i] forState:UIControlStateNormal];
        titleBtn.titleLabel.font = kFont(15);
        [titleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btnX = btnW * i;
        titleBtn.tag = i;
        titleBtn.frame = CGRectMake(btnX, 0, btnW, btnH);
          [titleBtn addTarget:self action:@selector(titleClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleScrollView addSubview:titleBtn];
        [self.titleBtnArray addObject:titleBtn];
        if (i == 0) {
            [self titleClicked:titleBtn];
        }
    }
    
    self.titleScrollView.contentSize = CGSizeMake(count * btnW, 0);
    self.contentScrollView.contentSize = CGSizeMake(count * kScreenWidth, 0);
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitlesScrollView];
    [self setupIndexLine];
    [self setupContentScrollView];
    [self setupAllChildViewController];
    [self setupAllTitle];
    
        
}

- (void)titleClicked:(UIButton *)btn {
    NSInteger i = btn.tag ;
    [self selectButton:btn];
    [self setupViewController:i];
    self.contentScrollView.contentOffset = CGPointMake(i * kScreenWidth, 0);
    
}

- (void)selectButton:(UIButton *)btn {
    _selectBtn.transform = CGAffineTransformIdentity;
    [_selectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self setupTitleCenter:btn];
    
    btn.transform = CGAffineTransformMakeScale(1.2, 1.2);
    _selectBtn = btn;
}

- (void) setupViewController:(NSInteger)index {
    UIViewController *vc = self.childViewControllers[index];
    if (vc.isViewLoaded) {
        return;
    }
    if (vc.view.superview) {
        return;
    }
    CGFloat x = index * kScreenWidth;
    vc.view.frame = CGRectMake(x, 0, kScreenWidth, self.contentScrollView.frame.size.height);
    [self.contentScrollView addSubview:vc.view];
    

}

- (void) setupTitleCenter:(UIButton *)btn {
    
    CGFloat btnX = btn.frame.origin.x;
    CGFloat offsetX = btn.center.x - kScreenWidth * 0.5;
   
    if (offsetX < 0) {
        offsetX = 0;
    }
    CGFloat maxOffsetX = self.titleScrollView.contentSize.width - kScreenWidth;
    if (offsetX > maxOffsetX) {
        offsetX = maxOffsetX;
    }
   
    
    [self.titleScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    
    if (offsetX < 0) {
        return;
    }
    [UIView animateWithDuration:0.25f animations:^{
        self.indexLine.frame = CGRectMake(btnX-offsetX, TitleScrollH, 80, 1);
    }];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger i = self.contentScrollView.contentOffset.x / kScreenWidth;
    
    UIButton *titleBtn = self.titleBtnArray[i];
    [self selectButton:titleBtn];
    [self setupViewController:i];


}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
